//
//  SelectTableView.m
//  photoLibrayIos8
//
//  Created by testbetta on 4/22/16.
//  Copyright © 2016 jing zhao. All rights reserved.
//

#import "SelectTableView.h"
#import "SelectTableViewCell.h"
#import "SelectTitleTableViewCell.h"
#import "PickerHeader.h"
#import "NSString+LocalString.h"
#import "UIColor+LocalColor.h"

@interface SelectTableView ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)NSMutableArray *arrayFetchData;

@end

@implementation SelectTableView

-(void)configureData:(NSArray*)arrayFetchData andDelegateSelect:(id<DelegateSelectTableView>)delegate {
    self.delegateSelect = delegate;
    self.arrayFetchData =[NSMutableArray arrayWithArray:arrayFetchData];
    [self.tableView registerNib: [UINib nibWithNibName:DEFINESELECTTABLECLASSNAME bundle:[NSBundle mainBundle]]forCellReuseIdentifier:STATICTABLEVIEWCELL];
    [self.tableView registerNib:[UINib nibWithNibName:SELECTTITLETABLEVIEWCELL bundle:[NSBundle mainBundle]] forCellReuseIdentifier:INDENTIFYSELECTTITLETABLEVIEWCELL];
    [self.tableView reloadData];
}

#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 38.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic =[self.arrayFetchData objectAtIndex:indexPath.row];
    [self.delegateSelect delegateSelect:dic];
}

#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayFetchData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *tableCell;
    NSDictionary *dic =[self.arrayFetchData objectAtIndex:indexPath.row];
    if ([[dic allKeys]containsObject:KEYPHASSETCOLLECTION]) {
        SelectTitleTableViewCell *tableCellTitle = [tableView dequeueReusableCellWithIdentifier:INDENTIFYSELECTTITLETABLEVIEWCELL];
        PHAssetCollection *collection = [dic objectForKey:KEYPHASSETCOLLECTION];
        NSArray *array = [dic objectForKey:KEYPHFETCHRESULT];
        tableCellTitle.titleLabel.text = [NSString stringWithFormat:@"%@ (%lu photos)",[NSString transformAblumTitle:collection.localizedTitle],(unsigned long)[array count]];
        tableCell = tableCellTitle ;
    }
    else{
        SelectTableViewCell *tableCellSelect = [tableView dequeueReusableCellWithIdentifier:STATICTABLEVIEWCELL];
        if (indexPath.row == [self.arrayFetchData count]-1)
              tableCellSelect.lineSpace.hidden = YES;
        tableCellSelect.titleLabel.text =  dic[@"subject"];
        tableCell = tableCellSelect;
    }
    tableCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return tableCell;
}

@end
